package com.jjtm.common.core.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @author wangbaofeng
 */
public class RandomUtils {

    private static String getString(int length, String base) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取从a至z，长度为length随机数
     * @param length 生成字符串的长度
     * @return String
     */
    public static String getRandomStr(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz";
        return getString(length, base);
    }



    /**
     * 获取范围内int值
     *
     * @param max 获取范围内int值
     * @param min 获取范围内int值
     * @return int
     */
    public static int getRandomRange(int max, int min) {
        return (int) (Math.random() * (max - min) + min);
    }

    /**
     * 获取随机长度随机字符
     *
     * @return String
     */
    public static String getRandomString(int length, String base) {
        // length表示生成字符串的长度
        return getString(length, base);
    }

    /**
     * 获取随机长度随机字符
     *
     * @param length 生成字符串的长度
     * @return String
     */
    public static String getRandomString(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        return getString(length, base);
    }

    /**
     * 获取随机长度随机数字
     *
     * @param length 生成字符串的长度
     * @return String
     */
    public static String getRandomNumString(int length) {
        String base = "0123456789";
        return getString(length, base);
    }

    /**
     * 返回随机数组
     *
     * @param start 开始值
     * @param end   结束值
     * @return int[]
     */
    public static int[] getRangRandom(int start, int end) {
        return getRangRandom(start, end, end - start + 1);
    }

    /**
     * 返回指定范围指定个数的不重复随机数。
     *
     * @param start start
     * @param end end
     * @param num num
     * @return int[]
     */
    public static int[] getRangRandom(int start, int end, int num) {

        int length = end - start + 1;
        // 参数不合法
        if (length < 1 || num > length) {
            return null;
        } else {
            int[] numbers = new int[length];
            int[] result = new int[num];
            // 循环赋初始值
            for (int i = 0; i < length; i++) {
                numbers[i] = i + start;
            }
            Random random = new Random();
            // 取randomMax次数
            for (int i = 0; i < num; i++) {
                // 随机获取取数的位置
                int m = random.nextInt(length - i) + i;
                result[i] = numbers[m];
                // 交换位置
                int temp = numbers[m];
                numbers[m] = numbers[i];
                numbers[i] = temp;
            }
            return result;
        }
    }

    /**
     * 生成验证码
     *
     * @return String
     */
    public static String verifyCode(int num) {
        StringBuilder sb = new StringBuilder();
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < num; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
}
